﻿using FastORM.Definition;
using FastORM.Entity;
using FastORM.Handle;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Threading;

namespace FastORM.Realization
{
    public class MySQLExecute : IFastExecute
    {
        public DbConnection CreateConnection(string ConnectionString)
        {
            string TransitionID = Thread.CurrentThread.ManagedThreadId.ToString();
            if (TransitionManage.ContainsTransition(TransitionID))
            {
                return TransitionManage.GetTransition(TransitionID).Transition.Connection;
            }
            else
            {
                return new MySqlConnection(ConnectionString);
            }
        }
        public int ExecuteNoQuery(string ConnectionString, string SQLText, List<ParamEntity> Params)
        {
            int DataCount = 0;
            string TransitionID = Thread.CurrentThread.ManagedThreadId.ToString();
            bool IsExecuteInTransition = TransitionManage.ContainsTransition(TransitionID);
            MySqlConnection Conn = (MySqlConnection)CreateConnection(ConnectionString);
            try
            {
                if (Conn.State != ConnectionState.Open)
                {
                    Conn.Open();
                }
                MySqlCommand Command = new MySqlCommand(SQLText, Conn);
                if (Params != null)
                {
                    foreach (ParamEntity item in Params)
                    {
                        Command.Parameters.AddWithValue(item.ParamName, item.ParamValue);
                    }
                }
                if (IsExecuteInTransition)
                {
                    Command.Transaction = (MySqlTransaction)TransitionManage.GetTransition(TransitionID).Transition;
                }
                DataCount = Command.ExecuteNonQuery();

            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if (!IsExecuteInTransition)
                {
                    Conn.Close();
                    Conn.Dispose();
                }
            }
            return DataCount;
        }
        public DataTable ExecuteQuery(string ConnectionString, string SQLText, List<ParamEntity> Params)
        {
            DataSet Set = new DataSet();
            using (MySqlConnection Conn = new MySqlConnection(ConnectionString))
            {
                Conn.Open();
                MySqlCommand Command = new MySqlCommand(SQLText, Conn);
                if (Params != null)
                {
                    foreach (ParamEntity item in Params)
                    {
                        Command.Parameters.AddWithValue(item.ParamName, item.ParamValue);
                    }
                }
                DbDataAdapter Adapter = new MySqlDataAdapter(Command);
                Adapter.Fill(Set);
            }
            if (Set.Tables.Count>0)
            {
                return Set.Tables[0];
            }
            else
            {
                return null;
            }
            
        }
        public object ExecuteScar(string ConnectionString, string SQLText, List<ParamEntity> Params)
        {
            Object DataResult = null;
            using (MySqlConnection Conn = new MySqlConnection(ConnectionString))
            {
                Conn.Open();
                MySqlCommand Command = new MySqlCommand(SQLText, Conn);
                if (Params != null)
                {
                    foreach (ParamEntity item in Params)
                    {
                        Command.Parameters.AddWithValue(item.ParamName, item.ParamValue);
                    }
                }
                DataResult = Command.ExecuteScalar();
            }
            return DataResult;
        }
    }
}
